package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * @author DL5O
 * @version 1.0
 * 分类管理
 */
@RestController
@Slf4j
@RequestMapping("/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;


    /**
     * 新增分类
     *
     * @param category
     * @return
     */
    @CacheEvict(value = "categoryCache", allEntries = true)
    @PostMapping
    public R<String> save(@RequestBody Category category) {
        //log.info("保存套餐/菜品方法控制器被调用..");
        boolean flag = categoryService.save(category);
        return flag ? R.success("新增成功") : R.error("新增失败");
    }

    /**
     * 分页查询
     *
     * @param page     当前页号
     * @param pageSize 当前每页显示多少
     * @return
     */
    @GetMapping("/page")
    @Cacheable(value = "categoryCache", key = "'category_' + #page + '_' + #pageSize",unless = "#result.getCode() == 0 ")
    public R<Page<Category>> getPage(Integer page, Integer pageSize) {
        log.info("分页查询：page={},pageSize={}", page, pageSize);
        Page<Category> iPage = new Page<>();
        LambdaQueryWrapper<Category> qw = new LambdaQueryWrapper<>();
        qw.orderByAsc(Category::getSort);
        iPage = categoryService.page(iPage, qw);
        return iPage != null ? R.success(iPage) : R.error("未知错误");
    }

    /**
     * 删除分类/套餐分类
     * @return R
     */
    @CacheEvict(value = "categoryCache", allEntries = true)
    @DeleteMapping
    public R<String> delete(Long ids) {
        //boolean flag = categoryService.removeById(ids);
        categoryService.remove(ids);
        return R.success("成功");
    }


    /**
     * 更新分类
     * @param category
     * @return
     */
    @CacheEvict(value = "categoryCache", allEntries = true)
    @PutMapping
    public R<String> update(@RequestBody Category category) {
        boolean flag = categoryService.updateById(category);
        return flag ? R.success("修改成功") : R.error("修改失败");
    }


    /**
     * 获取有分类信息
     * @param type 是套餐分类还是菜品分类 1为菜品分类，2位套餐分类
     * @return
     */
    @GetMapping("/list")
    @Cacheable(value = "categoryCache",key = "'categoryList_'+ #type",unless = "#result.getCode() == 0 ")
    public R<List<Category>> getList(Integer type) {
        List<Category> list = categoryService.getList(type);
        return list != null ? R.success(list) : R.error("查询失败");
    }

}
